﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using YArchitech.Revit;
using YJKMaterialStatistic;

namespace YJKStatisticTable.FastStatisticTable.Utility
{
	internal class GeneralSheetScheduleGrid
	{
		public GeneralSheetScheduleGrid(Document doc, GridSetCommonInfo gridSetInfo, ref FlexCell.Grid grid)
		{
			this.m_grid = grid;
			this.m_doc = doc;
			this.m_gridSet = gridSetInfo;
		}

		public bool GetStatisticGrid()
		{
			return true;
		}

		private void StatisticData()
		{
			if (this.m_doc == null)
			{
				return;
			}
			try
			{
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
				IList<Element> list = filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_TitleBlocks).ToElements();
				int num = 0;
				foreach (Element element in list)
				{
					if (element != null)
					{
						num++;
						List<string> list2 = new List<string>();
						list2.Insert(0, "");
						list2.Insert(1, num.ToString());
						string item = element.GetParameter(BuiltInParameter.SHEET_NUMBER).AsString();
						list2.Insert(2, item);
						item = element.GetParameter(BuiltInParameter.SHEET_NAME).AsString();
						list2.Insert(3, item);
						this.m_dicData.Add(num.ToString(), list2);
					}
				}
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
			}
		}

		private bool SetDataToGrid()
		{
			if (this.m_dicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Cell(2, 1).Text = this.m_gridSet.GridName;
			this.m_grid.Cell(2, 1).FontSize = this.m_gridSet.TitleFontSize;
			int num = 5;
			this.m_grid.Range(num + 1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			Dictionary<string, List<string>>.Enumerator enumerator = this.m_dicData.GetEnumerator();
			int count = this.m_dicData.Count;
			if (count == 0)
			{
				YJKMessageBox.Show("没有找到统计数据");
				return false;
			}
			this.m_grid.Rows = num + count + 1;
			foreach (KeyValuePair<string, List<string>> keyValuePair in this.m_dicData)
			{
				List<string> value = keyValuePair.Value;
				num++;
				for (int i = 1; i < value.Count; i++)
				{
					this.m_grid.Cell(num, i).Text = value[i];
					this.m_grid.Cell(num, i).Alignment = AlignmentEnum.CenterCenter;
				}
			}
			this.m_grid.Range(6, 2, num, this.m_grid.Cols - 1).Sort(2, SortTypeEnum.Text, SortEnum.Ascending);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Cell(1, 1).FontName = this.m_gridSet.FontName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSet.FontSize;
			this.m_grid.Range(6, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSet.FontName;
			this.m_grid.Range(6, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSet.FontSize;
			for (int j = 1; j < this.m_grid.Rows; j++)
			{
				this.m_grid.Row(j).AutoFit();
			}
			if (!this.m_gridSet.SetColWidth)
			{
				for (int k = 1; k < this.m_grid.Cols; k++)
				{
					this.m_grid.Column(k).AutoFit();
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private FlexCell.Grid m_grid;

		private Document m_doc;

		private GridSetCommonInfo m_gridSet;

		private Dictionary<string, List<string>> m_dicData = new Dictionary<string, List<string>>();

		private List<ViewPlan> m_lstViewPlan;
	}
}
